const Router = require('@koa/router')
const router = new Router()

const { commodity } = require('../vri')
const db = require('../db')

/**获取该用户的店铺id */
async function myShop(userid) {
  const data = await db.first({
    form: 'shop',
    where: {
      userid,
    },
  })
  if (data) return data.id
}

// 获取用户商品
router.get('/my', async (ctx) => {
  const { user } = ctx.state
  if (!user) return ctx.error(401)
  const { query } = ctx
  const { adopt, error, value } = commodity.search.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error(401)
  const { qw, page, size, classifyid, brandid, recycle } = value
  const where = {
    classifyid,
    shopid: value.all ? undefined : shopid,
    brandid,
    deleteAt: recycle,
    name: qw
      ? {
          op: 'LIKE',
          val: qw,
        }
      : undefined,
  }
  const data = await db.find({
    form: 'commodity',
    virtual: ['cover', 'shop', 'brand', 'classify'],
    where,
    limit: {
      page,
      size,
    },
  })
  const total = await db.count({
    form: 'commodity',
    where,
  })
  ctx.body = {
    code: 200,
    data,
    total,
  }
})
// 搜索商品
router.get('/search', async (ctx) => {
  const { query } = ctx
  const { adopt, error, value } = commodity.search.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { qw, page, size, classifyid, brandid } = value
  const where = {
    publish: 1,
    classifyid,
    brandid,
    'name`,`tags': qw
      ? {
          op: 'LIKE',
          val: qw,
          met: 'CONCAT',
        }
      : undefined,
  }
  const data = await db.find({
    form: 'commodity',
    virtual: ['cover', 'shop', 'brand', 'classify', 'commentCount'],
    where,
    limit: {
      page,
      size,
    },
    order: {
      read: 'DESC',
      createdAt: 'DESC',
    },
  })
  const total = await db.count({
    form: 'commodity',
    where,
  })
  ctx.body = {
    code: 200,
    data,
    total,
  }
})

// 添加
router.post('/add', async (ctx) => {
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = commodity.add.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error(401)
  const { lastId: id } = await db.insert({
    form: 'commodity',
    value: {
      ...value,
      deleteAt: 0,
      shopid,
    },
  })
  ctx.body = {
    code: 200,
    msg: '添加成功',
    id,
  }
})

// 修改
router.post('/modify', async (ctx) => {
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = commodity.modify.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error(401)
  const { id } = value
  delete value.id
  const { affectedRows } = await db.update({
    form: 'commodity',
    where: {
      id,
      // shopid,
    },
    set: value,
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 从id获取
router.get('/fromid', async (ctx) => {
  const { query } = ctx
  const { adopt, error, value } = commodity.fromid.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const data = await db.first({
    form: 'commodity',
    virtual: ['shop', 'brand'],
    where: {
      id: value.id,
    },
  })
  if (!data) return ctx.error(431)
  await db.update({
    form: 'commodity',
    set: '`read` = `read` + 1',
    where: {
      id: value.id,
    },
  })
  ctx.body = {
    code: 200,
    data,
  }
})

// 删除
router.post('/del', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 表单验证
  const { body } = ctx.request
  const { adopt, error, value } = commodity.del.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 店铺验证
  const shop = await db.count({
    form: 'shop',
    where: {
      userid: user.id,
      id: value.shopid,
    },
  })
  if (!shop) return ctx.error('401')
  const { affectedRows } = await db.remove({
    form: 'commodity',
    where: {
      id: value.id,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 恢复
router.post('/recycle', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  // 表单验证
  const { body } = ctx.request
  const { adopt, error, value } = commodity.del.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 店铺验证
  const shop = await db.count({
    form: 'shop',
    where: {
      userid: user.id,
      id: value.shopid,
    },
  })
  if (!shop) return ctx.error('401')
  const { affectedRows } = await db.update({
    form: 'commodity',
    set: {
      deleteAt: 0,
    },
    where: {
      id: value.id,
      deleteAt: 1,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 批量删除
router.post('/dels', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = commodity.dels.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 店铺验证
  const shop = await db.count({
    form: 'shop',
    where: {
      userid: user.id,
      id: value.shopid,
    },
  })
  if (!shop) return ctx.error('401')
  const { affectedRows } = await db.remove({
    form: 'commodity',
    where: {
      id: value.ids,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 发布商品
router.post('/publish', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = commodity.publish.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 店铺验证
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error('401')
  const { affectedRows } = await db.update({
    form: 'commodity',
    set: {
      publish: 1,
    },
    where: {
      id: value.id,
      shopid,
    },
  })
  if (!affectedRows) return ctx.error(431)
  ctx.body = {
    code: 200,
    msg: '发布成功',
  }
})

// 下架商品
router.post('/downshelf', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = commodity.publish.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  // 店铺验证
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error('401')
  const { affectedRows } = await db.update({
    form: 'commodity',
    set: {
      publish: 0,
    },
    where: {
      id: value.id,
      shopid,
    },
  })
  if (!affectedRows) return ctx.error(431)
  ctx.body = {
    code: 200,
    msg: '下架成功',
  }
})

// 获取评论
router.get('/comment', async (ctx) => {
  const { user } = ctx.state
  // 验证参数
  const { query } = ctx
  const { adopt, error, value } = commodity.comment.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })

  const data = await db.find({
    form: 'comment',
    where: {
      commodityid: value.id,
    },
    virtual: ['user', 'agree', 'replyCount', 'isAgree'],
    param: user,
  })
  ctx.body = {
    code: 200,
    data,
  }
})

// 获取商品数量
router.get('/count', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const data = await db.first({
    form: 'shop',
    where: {
      userid: user.id,
    },
  })
  if (!data) {
    ctx.body = {
      code: 200,
      count: 0,
    }
    return
  }
  const count = await db.count({
    form: 'commodity',
    where: {
      shopid: data.id,
    },
  })
  ctx.body = {
    code: 200,
    count,
  }
})

// 访问量
router.get('/read', async (ctx) => {
  // 验证登陆
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const shopid = await myShop(user.id)
  if (!shopid) return ctx.error(431)
  // 查询
  const data = await db.find({
    form: 'commodity',
    field: ['read'],
    where: {
      shopid,
    },
  })
  if (!data) {
    ctx.body = {
      code: 200,
      count: 0,
    }
    return
  }
  let count = 0
  data.forEach((e) => {
    count += e.read
  })
  ctx.body = {
    code: 200,
    count,
  }
})

module.exports = router.routes()
